/**
 * 
 */
package com.jie.eclipse.pan.gef.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.swt.widgets.TableItem;

import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.swtitem.actiongroup.BaseActionGroup;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class GridSeachActionGroup extends BaseActionGroup{
	/**
	 * @param baseDialog
	 * @param action
	 * @param tabid
	 */
	
	public GridSeachActionGroup(Object obj,String key,String action) {
		super(obj,key,action);
	}

	private class Insert1Action extends Action{
		public Insert1Action() {
			this.setImageDescriptor(getImageDesc("icon_addrow.gif"));
			this.setText("添加查询条件");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			CheckboxTableViewer ctv2 =  (CheckboxTableViewer)getData().gpov(getKey()+"1ctv2");//已选栏位表格选择器
			
			if(ctv1 == null || ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			List<Object> list = new ArrayList<Object>();
			if("Crud1LeftPage_SQL".equals(getAction())){//key = Crud1LeftPage
				//选择表格里的栏位
				for (int i = 0; i < ctv1.getCheckedElements().length; i++) {
					BaseDataEntity row = new BaseDataEntity(); 
					((BaseDataEntity)ctv1.getCheckedElements()[i]).CopyTo(row, false);
					row.spsv("name", row.gpsv("cid"));
					row.spsv("width", "100");
					row.spsv("xtype", "text");
					row.spsv("xtype_text", "文本框");
					row.spsv("required", "false");
					row.spsv("required_text", "Y");
					row.spsv("detail", "配置");
					row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
					row.spsv("op", "=");
					row.spsv("op_text", "等于");
					row.spsv("tablepix", "");
					row.spsv("columnalias", row.gpsv("cid").toLowerCase());
					list.add(row);
				}
			}else if("GridCrudPage_SQL".equals(getAction())){//key = Crud1LeftPage
				//选择表格里的栏位
				for (int i = 0; i < ctv1.getCheckedElements().length; i++) {
					BaseDataEntity row = new BaseDataEntity(); 
					((BaseDataEntity)ctv1.getCheckedElements()[i]).CopyTo(row, false);
					row.spsv("name", row.gpsv("cid"));
					row.spsv("name", row.gpsv("cid"));
					row.spsv("width", "100");
					row.spsv("xtype", "text");
					row.spsv("xtype_text", "文本框");
					row.spsv("required", "false");
					row.spsv("required_text", "Y");
					row.spsv("detail", "配置");
					row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
					row.spsv("op", "=");
					row.spsv("op_text", "等于");
					row.spsv("tablepix", "");
					row.spsv("columnalias", row.gpsv("cid").toLowerCase());
					list.add(row);
				}
			} 
			ctv2.add(list.toArray());
		}
	}
	
	private class Insert11Action extends Action{
		public Insert11Action() {
			this.setImageDescriptor(getImageDesc("icon_addrow.gif"));
			this.setText("添加表格栏位");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			CheckboxTableViewer ctv2 =  (CheckboxTableViewer)getData().gpov(getKey()+"2ctv2");//已选栏位表格选择器
			
			if(ctv1 == null || ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			List<Object> list = new ArrayList<Object>();
			if("Crud1LeftPage_SQL".equals(getAction())){
				//选择表格里的栏位
				for (int i = 0; i < ctv1.getCheckedElements().length; i++) {
					BaseDataEntity row = new BaseDataEntity(); 
					((BaseDataEntity)ctv1.getCheckedElements()[i]).CopyTo(row, false);
					row.spsv("name", row.gpsv("cid"));
					row.spsv("width", "100");
					list.add(row);
				}
			}else if("GridCrudPage_SQL".equals(getAction())){
				//选择表格里的栏位
				for (int i = 0; i < ctv1.getCheckedElements().length; i++) {
					BaseDataEntity row = new BaseDataEntity(); 
					((BaseDataEntity)ctv1.getCheckedElements()[i]).CopyTo(row, false);
					row.spsv("name", row.gpsv("cid"));
					row.spsv("xtype", "");
					row.spsv("xtype_text", "--");
					row.spsv("editortype", "static");
					row.spsv("editortype_text", "静态");
					row.spsv("detail", "配置");				
					row.spov("detail_img","icons/gef/eclipse/thread_view.gif");
					list.add(row);
				}
			}
			ctv2.add(list.toArray());
		}
	}
	
	private class Insert2Action extends Action{
		public Insert2Action() {
			this.setImageDescriptor(getImageDesc("checkboxon.gif"));
			this.setText("全选");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			if(ctv1 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			for (int i = 0; i < ctv1.getTable().getItems().length; i++) {
				TableItem item = ctv1.getTable().getItems()[i];
				item.setChecked(true);
			}
		}
	}
	
	private class Insert3Action extends Action{
		public Insert3Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("全否");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			if(ctv1 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv1.getTable().getItems().length; i++) {
				TableItem item = ctv1.getTable().getItems()[i];
				item.setChecked(false);
			}
			
		}
	}
	
	
	
	private class Insert4Action extends Action{
		public Insert4Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("反选");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			if(ctv1 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv1.getTable().getItems().length; i++) {
				TableItem item = ctv1.getTable().getItems()[i];
				item.setChecked(!item.getChecked());
			}
			
		}
	}
	
	private class Insert5Action extends Action{
		public Insert5Action() {
			this.setImageDescriptor(getImageDesc("checkboxoff.gif"));
			this.setText("剔除公共栏位");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 =   (CheckboxTableViewer)getData().gpov(getKey()+"ctv1");//可选栏位表格选择器
			if(ctv1 == null ){
				Utils.print("表格选择器不正常");
				return;
			}
			
			for (int i = 0; i < ctv1.getTable().getItems().length; i++) {
				TableItem item = ctv1.getTable().getItems()[i];
				if(item.getChecked() 
						&& (","+QMDao.getInstance().getCommonColumns().toLowerCase()+",").indexOf(","+item.getText().toLowerCase()+",") >=0){
					item.setChecked(false);
				}
			}
		}
	}
	
	public void fillActionToolbars (ToolBarManager actionBarManager){
		Insert1Action action1 = new Insert1Action();
		actionBarManager.add(createActionContributionItem(action1));
		Insert11Action action11 = new Insert11Action();
		actionBarManager.add(createActionContributionItem(action11));
		Insert2Action action2 = new Insert2Action();
		actionBarManager.add(createActionContributionItem(action2));
		Insert5Action action5 = new Insert5Action();
		actionBarManager.add(createActionContributionItem(action5));
		Insert3Action action3 = new Insert3Action();
		actionBarManager.add(createActionContributionItem(action3));
		Insert4Action action4 = new Insert4Action();
		actionBarManager.add(createActionContributionItem(action4));
		actionBarManager.update(true);
	}
	
	
}
